package com.mozz.finally_work.servlet;

import com.mozz.finally_work.dao.BlogDao;
import com.mozz.finally_work.pojo.Blog;
import com.mozz.finally_work.utils.MybatisUtils;
import com.mozz.finally_work.utils.ParseMarkdown;
import org.apache.ibatis.session.SqlSession;
import org.pegdown.PegDownProcessor;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "BlogDetailServlet", value = "/BlogDetailServlet")
public class BlogDetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取参数
        String id = request.getParameter("id");
        // dao查询
        SqlSession session = MybatisUtils.getSession();
        BlogDao dao = session.getMapper(BlogDao.class);
        Blog blog = dao.queryBlogById(id);
       // 将 content由markdown转为html
        String parse = ParseMarkdown.parse(blog.getContent());
        blog.setContent(parse);
        session.close();
        // 请求转发
        request.setAttribute("blog", blog);
        request.getRequestDispatcher("blog_detail.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}
